
#import <UIKit/UIViewController.h>
#import "sdk/util/wechatutil/WXApi.h"
#import "sdk/util/wechatutil/WXApiObject.h"
#import "sdk/util/wechatutil/WechatAuthSDK.h"
#include "WeChatLoginIOSEX.h"
#include "sdk/message/Common.h"
#include "WeChatLoginIOS.h"
#include "WeChatLogin.h"


int reqType = 0;
std::string APP_SECRET	= "";
std::string APP_ID		= "";

std::string getAppSecret()
{
	return APP_SECRET;
}

std::string getAppID()
{
	return APP_ID;
}

void authIOS()
{
	reqType = 1;
	SendAuthReq *req = [[[SendAuthReq alloc] init] autorelease];
	req.scope = @"snsapi_userinfo";
	req.state = @"WeChatLoginTest";
	[WXApi sendReq:req];
}

void inviteIOS(const char* title,const char* content, int type, const char* url)
{
	NSLog(@"send invite ios");
	reqType = 2;
	WXMediaMessage *message = [WXMediaMessage message];
	message.title = [[NSString alloc] initWithUTF8String:title];
	message.description = [[NSString alloc] initWithUTF8String:content];
	[message setThumbImage:[UIImage imageNamed:@"icon.png"]];

	WXWebpageObject *ext = [WXWebpageObject object];
	ext.webpageUrl = [[NSString alloc] initWithUTF8String:url];
	message.mediaObject= ext;

	SendMessageToWXReq *req = [[[SendMessageToWXReq alloc] init] autorelease];
	req.bText = NO;
	req.message = message;
	if(type == 0){
		req.scene = WXSceneSession;
	}else{
		req.scene = WXSceneTimeline;
	}

	[WXApi sendReq:req];
}

void codeResponseIOS(const char* code,const char* appId,const char* appSecret)
{
	WH_LogErr("WeChatLoginIOS", U2F("coderesponse").c_str());
	WH_LogErr("WeChatLoginIOS",StringUtils::format("code:%s  appid:%s  appsecret:%s",code,appId,appSecret).c_str());
	WeChatLogin::getInstance()->setCode(code);
	WeChatLogin::getInstance()->setAppId(appId);
	WeChatLogin::getInstance()->setSecret(appSecret);

	WeChatLogin::getInstance()->requestAccessToken();
}

UIImage* zoomImage(UIImage* image, CGSize reSize)
{
	CGSize size_max = CGSizeMake(reSize.width, reSize.height);

	UIGraphicsBeginImageContext(size_max);
	[image drawInRect:CGRectMake(0,0,size_max.width, size_max.height)];
	UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();

	return scaledImage;
}

void resultShareIOS(const char* title,const char* content,const char* path)
{
    NSLog(@"send invite ios");
    reqType = 3;
	NSString *url = [[NSString alloc] initWithUTF8String:path];

    WXMediaMessage *message = [WXMediaMessage message];

	NSData* imgData		= [NSData dataWithContentsOfFile:url];
	UIImage* img		= [UIImage imageWithData: imgData];
	CGRect rect			= [UIScreen mainScreen].bounds;
	CGSize fraSize		= rect.size;
	float scale			= fraSize.width > fraSize.height ? (fraSize.width / fraSize.height) : (fraSize.height / fraSize.width);
	NSLog(@"scale screen = %f", scale);

	CGSize reSize		= CGSizeMake(80 * scale, 80);
	UIImage* scaleImg	= zoomImage(img, reSize);

    [message setThumbImage:scaleImg];

	WXImageObject *ext	= [WXImageObject object];
	ext.imageData		= imgData;

	UIImage *newimg		= zoomImage(img, CGSizeMake(800 * scale, 800));
	ext.imageData		= UIImageJPEGRepresentation(newimg, 0.3f);
	message.mediaObject = ext;
    
    SendMessageToWXReq *req = [[[SendMessageToWXReq alloc] init] autorelease];
    req.bText	= NO;
    req.message = message;
    req.scene	= WXSceneSession;
    
    [WXApi sendReq:req];
}

void InitWXKey(const char* szAppID, const char* pszSecret)
{
	APP_ID		= szAppID;
	APP_SECRET	= pszSecret;
}

bool isInstalledIOS()
{
	return [WXApi isWXAppInstalled];
}

void onResponse(BaseResp *resp)
{
	if (resp.errCode == WXSuccess) {
		//添加成功判断[成功]
		NSLog(@" on Response ios resp.errCode WXSuccess OK");
		if(reqType == 1)
		{
			SendAuthResp *resps = (SendAuthResp*)resp;
			NSString *code = resps.code;
			codeResponseIOS([code UTF8String], APP_ID.c_str(), APP_SECRET.c_str());
		}
		else if(reqType == 2)
		{
			WeChatLogin::getInstance()->onShareSuccess();
			NotificationCenter::getInstance()->postNotification(WECHAT_INVITE_SUCCESS,nullptr);
		}
		else if(reqType == 3)
		{
			NotificationCenter::getInstance()->postNotification(WECHAT_RESULT_SHARE_SUCCESS,nullptr);
		}
	}else{
		//添加成功判断[失败]
		NSLog(@" on Response ios resp.errCode WXSuccess Error");
	}
}
